package Offer2017;

import java.util.Stack;

/**
 * 栈的压入弹出序列
 * Created by 雷文康 on 2018/1/1.
 */
public class Test22 {
    public static void main(String[] args) {
        int[] pushArray = {1, 2, 3, 4, 5};
        int[] popArray = {5, 3, 4, 2, 1};
        System.out.println(isPopOrder(pushArray, popArray));
    }

    public static boolean isPopOrder(int[] pushArr, int[] popArr) {
        if (pushArr == null || popArr == null || pushArr.length == 0 || popArr.length == 0) {
            return false;
        }
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushArr.length; i++) {
            stack.push(pushArr[i]);
            while (j < popArr.length && popArr[j] == stack.peek()) {
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty();
    }
}
